<?php
/**
 * 注意：本内容仅限于博也公司内部传阅,禁止外泄以及用于其他的商业目的
 * @author    hebidu<346551990@qq.com>
 * @copyright 2018 www.itboye.com Boye Inc. All rights reserved.
 * @link      http://www.itboye.com/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 * Revision History Version
 ********1.0.0********************
 * file created @ 2018-01-09 14:30
 *********************************
 ********1.0.1********************
 *
 *********************************
 */

namespace app\src\sunsun\common\logic;


use app\src\base\logic\BaseLogic;
use think\Db;

class AllDeviceLocLogic extends BaseLogic
{
    /**
     * @param int $size
     * @param bool $queryNull
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryAllNewDevice($size = 10, $queryNull = true)
    {
        $query = Db::table("itboye_all_device_loc")
            ->field("v_dev.did,v_dev.last_login_time,v_dev.last_login_ip as ip,v_dev.d_type,v_dev.delay_avg as delay ")
            ->join(["v_all_device_info"=>"v_dev"], "v_dev.did = itboye_all_device_loc.did", "RIGHT");
        if ($queryNull) {
            $query = $query->whereNull("itboye_all_device_loc.ip", "OR");
            return $query->fetchSql(false)->selectInsert("did,last_login_time,ip,d_type,delay ", "__ALL_DEVICE_LOC__");
        } else {
            $query->where(["itboye_all_device_loc.ip"=>['exp', ' <> `v_dev`.`last_login_ip`']]);
            $query->whereOr(["itboye_all_device_loc.delay"=>['exp', ' <> `v_dev`.`delay_avg`']]);
        }
        return $query->limit(0, $size)->fetchSql(false)->select();
    }
}